package com.youthburin.base.util;

import java.io.Serializable;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;

@SuppressWarnings("rawtypes")
public class DwzPagination implements Serializable {
	private static final long serialVersionUID = 1L;
	public static final String SEARCHFILEDNAME_PREFIX = "YOUTH_BURIN_SFN_";
	public static final String SEARCHFIELDOPNAME_PREFIX = "YOUTH_BURIN_SFO_";
	/** DwzPagination存放在HttpservletRequest属性中的key */
	public static final String DWZPAGINATION_PROPERTY_KEY = "page";
	private int pageNum = 1;// 当前页
	private int numPerPage;// 每页数据量
	private long totalCount;// 总数据量
	private int pageNumShown = 10;// 页标显示个数
	private String orderField;
	private String orderDirection;
	private final int DEFAULT_PAGE_SIZE = 10;
	private List list;
	private final String[] numPerPageList = new String[] { "10", "20", "50" };
	private List<DwzCondition> conditionList;

	@Override
	public String toString() {
		return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
	}

	public int getPageNum() {
		return pageNum;
	}

	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}

	public int getNumPerPage() {
		return numPerPage;
	}

	public void setNumPerPage(int numPerPage) {
		this.numPerPage = numPerPage;
	}

	public String getOrderField() {
		return orderField;
	}

	public void setOrderField(String orderField) {
		this.orderField = orderField;
	}

	public String getOrderDirection() {
		return orderDirection;
	}

	public void setOrderDirection(String orderDirection) {
		this.orderDirection = orderDirection;
	}

	public Pageable exportSpringDataJpaPageable() {
		if (getNumPerPage() == 0)
			setNumPerPage(DEFAULT_PAGE_SIZE);

		if (StringUtils.isNotBlank(getOrderDirection()) && StringUtils.isNotBlank(getOrderField())) {
			Sort sort = new Sort(Direction.fromString(getOrderDirection()), getOrderField());
			return new PageRequest(getPageNum() - 1, getNumPerPage(), sort);
		} else {
			return new PageRequest(getPageNum() - 1, getNumPerPage());
		}

	}

	public void importPageable(Page page) {
		this.list = page.getContent();
		this.totalCount = page.getTotalElements();
	}

	public List getList() {
		return list;
	}

	public void setList(List list) {
		this.list = list;
	}

	public long getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(long totalCount) {
		this.totalCount = totalCount;
	}

	public int getPageNumShown() {
		return pageNumShown;
	}

	public void setPageNumShown(int pageNumShown) {
		this.pageNumShown = pageNumShown;
	}

	public List<DwzCondition> getConditionList() {
		return conditionList;
	}

	public void setConditionList(List<DwzCondition> conditionList) {
		this.conditionList = conditionList;
	}

	public String[] getNumPerPageList() {
		return numPerPageList;
	}
}
